<?php

namespace ms\modules\magic\controllers;

use yii\web\Controller;
use common\models\SysSkuCount;
use common\models\BaseSupplierGoods;
use common\models\BaseSite;


class MagicApiController extends Controller
{
    
    /**
     * @description:51魔方获取模块sku接口
     * @return: return_type
     * @author: leo
     * @date: 2015年7月21日下午2:37:50
     * @modified_date: 2015年7月21日下午2:37:50
     * @modified_user: leo
     * @review_user:
    */
    public function actionModelSku()
    {
        $data = SysSkuCount::find()->where(['time'=>date('Y-m-d',time())])->asArray()->all();
        return json_encode($data);
    }
    
    /**
     * @description:51魔方获取品牌sku接口
     * @return: return_type
     * @author: leo
     * @date: 2015年7月21日下午2:52:55
     * @modified_date: 2015年7月21日下午2:52:55
     * @modified_user: leo
     * @review_user:
    */
    public function actionBrandSku(){
        $site = BaseSite::find()->asArray()->all();
        foreach ($site as $val){
            $site_id = $val['id'];
            $sql = "SELECT t1.brand_id,count(*) as count FROM dh_base_supplier_goods AS t1 LEFT JOIN dh_base_supp_goods_site AS t2 ON t1.id = t2.supplier_goods_id
                    WHERE t2.site_id = $site_id AND t2.status = 1 AND t1.status = 1 AND t1.is_deleted = 0 AND t1.enable = 1 AND t1.num_avai > 0  GROUP BY t1.brand_id";
            $data[$site_id] = BaseSupplierGoods::findBySql($sql)->asArray()->all();
        }
        return  json_encode($data);
    }
    
    /**
     * @description:51魔方获取类型sku接口
     * @return: string
     * @author: leo
     * @date: 2015年7月24日下午2:26:10
     * @modified_date: 2015年7月24日下午2:26:10
     * @modified_user: leo
     * @review_user:
    */
    public function actionTypeSku(){
        $site = BaseSite::find()->asArray()->all();
        foreach ($site as $val){
            $site_id = $val['id'];
        $sql = "SELECT t1.type_id,count(*) as count FROM dh_base_supplier_goods AS t1 LEFT JOIN dh_base_supp_goods_site AS t2 ON t1.id = t2.supplier_goods_id
                WHERE t2.site_id = $site_id AND t2.status = 1 AND t1.status = 1 AND t1.is_deleted = 0 AND t1.enable = 1 AND t1.num_avai > 0  GROUP BY t1.type_id";
            $data[$site_id] = BaseSupplierGoods::findBySql($sql)->asArray()->all();
        }
        return json_encode($data);
    }
}
